# encoding:utf-8
import os
import sys
import logging
import shutil
import platform
import copy
from pylibs.lpkg.compile_scripts import compile_script


SEP_DIR_ZIPFILE = dict(
    config = dict(
        jitc32 = "config.lpkg",
        jitc64 = "config64.lpkg",
        luac = "config.upkg"
    )
)

DEFAULT_ZIPFILE = dict(
    default = dict(
        jitc32 = "game.lpkg",
        jitc64 = "game64.lpkg",
        luac = "game.upkg"
    )
)

def path_fmt(path):
    return path.replace("/", os.sep)

def luajit_pack(frompath, tarpath, packtype="jitc"):
    '''进行脚本打包
        @frompath 源脚本目录
        @tarpath 目标目录
        @packtype jit类型, "jitc", "luac", "onlyzip, jitmix(extend)"
    '''
    global SEP_DIR_ZIPFILE
    global DEFAULT_ZIPFILE
    srcpath = frompath
    psrcpath = os.path.dirname(srcpath)
    tmpsep     = path_fmt(psrcpath + "/template_")
    tmpdefault = path_fmt(psrcpath + "/template_game")

    sepps = []

    # ===============Step1. sep template dirs=================
    # sep dirs-->
    for sepkey, val in SEP_DIR_ZIPFILE.items():
        tmpp = tmpsep + sepkey
        os.mkdir(tmpp)
        shutil.move(os.path.join(srcpath, sepkey), \
                        tmpp)
        sepps.append([sepkey, tmpp])

    # default-->
    os.renames(frompath, tmpdefault)
    # ===============Step2. get config ty packtype=================
    pconflist = []
    TP_CONF = dict(
        iszip=True,
        isjit=False,
        frompath="",
        tarfile="",
        bittype = "32bit"
    )

    seplen = len(SEP_DIR_ZIPFILE)
    if packtype == "jitc":
        for conflist in sepps:
            sconf = copy.copy(TP_CONF)
            sconf["isjit"] = True
            sconf["frompath"] = conflist[1]
            sconf["tarfile"] = os.path.join(tarpath, SEP_DIR_ZIPFILE[conflist[0]]["jitc32"])
            pconflist.append(sconf)
        
        gconf = copy.copy(TP_CONF)
        gconf["isjit"] = True 
        gconf["frompath"] = tmpdefault
        gconf["tarfile"] = os.path.join(tarpath, DEFAULT_ZIPFILE["default"]["jitc32"]) 
        pconflist.append(gconf)

    elif packtype == "jitmix":
        for conflist in sepps:
            sconf = copy.copy(TP_CONF)
            sconf["isjit"] = True
            sconf["frompath"] = conflist[1]
            sconf["tarfile"] = os.path.join(tarpath, SEP_DIR_ZIPFILE[conflist[0]]["jitc32"])
            sconf["bittype"] = "32bit"

            sconf2 = copy.copy(TP_CONF)
            sconf2["isjit"] = True
            sconf2["frompath"] = conflist[1]
            sconf2["bittype"] = "64bit"
            sconf2["tarfile"] = os.path.join(tarpath, SEP_DIR_ZIPFILE[conflist[0]]["jitc64"])

            pconflist.append(sconf)
            pconflist.append(sconf2)
        
        gconf = copy.copy(TP_CONF)
        gconf["isjit"] = True 
        gconf["frompath"] = tmpdefault
        gconf["tarfile"] = os.path.join(tarpath, DEFAULT_ZIPFILE["default"]["jitc32"]) 
        gconf["bittype"] = "32bit"

        gconf2 = copy.copy(TP_CONF)
        gconf2["isjit"] = True 
        gconf2["frompath"] = tmpdefault
        gconf2["bittype"] = "64bit"
        gconf2["tarfile"] = os.path.join(tarpath, DEFAULT_ZIPFILE["default"]["jitc64"])

        pconflist.append(gconf)
        pconflist.append(gconf2)
    
    sys.exit()
    for confobj in pconflist:
        logging.info("Packing the file -->>>>%s.It will last a little :3 hold on!", confobj["tarfile"])
        compile_script(confobj)


def hot_mkdir(tarpath, bigv, smallv):
    '''
    创建热更新包的目录结构,并且剪切lua包到正确路径
    @tarpath 目标目录
    @bigv 大版本号
    @smallv 小版本号
    '''
    despath = tarpath + "/{b}/{s}/res".format(b=bigv, s=smallv)
    cmd_mkdir = "mkdir " + despath
    os.system(cmd_mkdir)
